import _typeof from "@babel/runtime/helpers/esm/typeof";
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
var _excluded = ["children"],
    _excluded2 = ["children"];
import { jsx as _jsx } from "react/jsx-runtime";
import { genCopyable, isDeepEqualReact, isNil, LabelIconTip } from '@ant-design/pro-utils';
import { Space } from 'antd-v4';
import get from "rc-util/es/utils/get";
import React from 'react';
import { isMergeCell } from '.';
import cellRenderToFromItem from './cellRenderToFromItem';
/**
 * 增加了 icon 的功能 render title
 *
 * @param item
 */

export var renderColumnsTitle = function renderColumnsTitle(item) {
  var title = item.title;

  if (title && typeof title === 'function') {
    return title(item, 'table', _jsx(LabelIconTip, {
      label: null,
      tooltip: item.tooltip || item.tip
    }));
  }

  return _jsx(LabelIconTip, {
    label: title,
    tooltip: item.tooltip || item.tip,
    ellipsis: item.ellipsis
  });
};
/** 判断可不可编辑 */

function isEditableCell(text, rowData, index, editable) {
  if (typeof editable === 'boolean') {
    return editable === false;
  }

  return (editable === null || editable === void 0 ? void 0 : editable(text, rowData, index)) === false;
}
/**
 * 默认的 filter 方法
 *
 * @param value
 * @param record
 * @param dataIndex
 * @returns
 */


export var defaultOnFilter = function defaultOnFilter(value, record, dataIndex) {
  var recordElement = Array.isArray(dataIndex) ? get(record, dataIndex) : record[dataIndex];
  var itemValue = String(recordElement);
  return String(itemValue) === String(value);
};

var OptionsCell = /*#__PURE__*/function (_React$Component) {
  _inherits(OptionsCell, _React$Component);

  var _super = _createSuper(OptionsCell);

  function OptionsCell() {
    _classCallCheck(this, OptionsCell);

    return _super.apply(this, arguments);
  }

  _createClass(OptionsCell, [{
    key: "shouldComponentUpdate",
    value: function shouldComponentUpdate(nextProps) {
      var _this$props = this.props,
          children = _this$props.children,
          restProps = _objectWithoutProperties(_this$props, _excluded);

      var nextChildren = nextProps.children,
          restNextProps = _objectWithoutProperties(nextProps, _excluded2);

      return !isDeepEqualReact(restProps, restNextProps);
    }
  }, {
    key: "render",
    value: function render() {
      return _jsx(Space, {
        children: this.props.children(this.props.form)
      });
    }
  }]);

  return OptionsCell;
}(React.Component);
/**
 * 这个组件负责单元格的具体渲染
 *
 * @param param0
 */


export function columnRender(_ref) {
  var columnProps = _ref.columnProps,
      text = _ref.text,
      rowData = _ref.rowData,
      index = _ref.index,
      columnEmptyText = _ref.columnEmptyText,
      counter = _ref.counter,
      type = _ref.type,
      subName = _ref.subName,
      editableUtils = _ref.editableUtils;
  var action = counter.action,
      prefixName = counter.prefixName,
      editableForm = counter.editableForm;

  var _editableUtils$isEdit = editableUtils.isEditable(_objectSpread(_objectSpread({}, rowData), {}, {
    index: index
  })),
      isEditable = _editableUtils$isEdit.isEditable,
      recordKey = _editableUtils$isEdit.recordKey;

  var _columnProps$renderTe = columnProps.renderText,
      renderText = _columnProps$renderTe === void 0 ? function (val) {
    return val;
  } : _columnProps$renderTe;
  var renderTextStr = renderText(text, rowData, index, action);
  var mode = isEditable && !isEditableCell(text, rowData, index, columnProps === null || columnProps === void 0 ? void 0 : columnProps.editable) ? 'edit' : 'read';
  var textDom = cellRenderToFromItem({
    text: renderTextStr,
    valueType: columnProps.valueType || 'text',
    index: index,
    rowData: rowData,
    subName: subName,
    columnProps: _objectSpread(_objectSpread({}, columnProps), {}, {
      // 为了兼容性，原来写了个错别字
      // @ts-ignore
      entry: rowData,
      entity: rowData
    }),
    counter: counter,
    columnEmptyText: columnEmptyText,
    type: type,
    recordKey: recordKey,
    mode: mode,
    prefixName: prefixName,
    editableUtils: editableUtils
  });
  var dom = mode === 'edit' ? textDom : genCopyable(textDom, columnProps, renderTextStr);
  /** 如果是编辑模式，并且 renderFormItem 存在直接走 renderFormItem */

  if (mode === 'edit') {
    if (columnProps.valueType === 'option') {
      return _jsx(OptionsCell, {
        record: rowData,
        form: editableForm,
        children: function children(inform) {
          return editableUtils.actionRender(_objectSpread(_objectSpread({}, rowData), {}, {
            index: columnProps.index || index
          }), inform);
        }
      });
    }

    return dom;
  }

  if (!columnProps.render) {
    var isReactRenderNode = /*#__PURE__*/React.isValidElement(dom) || ['string', 'number'].includes(_typeof(dom));
    return !isNil(dom) && isReactRenderNode ? dom : null;
  }

  var renderDom = columnProps.render(dom, rowData, index, _objectSpread(_objectSpread({}, action), editableUtils), _objectSpread(_objectSpread({}, columnProps), {}, {
    isEditable: isEditable,
    type: 'table'
  })); // 如果是合并单元格的，直接返回对象

  if (isMergeCell(renderDom)) {
    return renderDom;
  }

  if (renderDom && columnProps.valueType === 'option' && Array.isArray(renderDom)) {
    return _jsx(Space, {
      size: 16,
      children: renderDom
    });
  }

  return renderDom;
}